const orderModel = require('../models/orderModel');
const cartModel = require('../models/cartModel');
const { sendSuccess, sendError } = require('../utils/response');

module.exports = {
  // 创建订单
  async createOrder(req, res) {
    try {
      const { id: userId } = req.user;
      const { address_id } = req.body;
      
      if (!address_id) {
        return sendError(res, '请选择收货地址', 400);
      }
      
      // 获取购物车商品
      const cartItems = await cartModel.getCartByUserId(userId);
      
      if (cartItems.length === 0) {
        return sendError(res, '购物车为空', 400);
      }
      // 创建订单
      const orderId = await orderModel.createOrder(
        userId,
        address_id,
        cartItems.map(item => ({
          product_id: item.product_id,
          quantity: item.quantity
        }))
      );
      

      // 获取订单详情
      const order = await orderModel.getOrderById(userId, orderId);
      sendSuccess(res, order, '订单创建成功', 201);
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 获取用户订单列表
  async getOrders(req, res) {
    try {
      const { id: userId } = req.user;
      const { page = 1, limit = 10 } = req.query;
      
      const { orders, pagination } = await orderModel.getOrdersByUserId(userId, page, limit);
      sendSuccess(res, { orders, pagination });
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 获取订单详情
  async getOrder(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      const order = await orderModel.getOrderById(userId, id);
      sendSuccess(res, order);
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 取消订单
  async cancelOrder(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      await orderModel.updateOrderStatus(userId, id, 'cancelled');
      const order = await orderModel.getOrderById(userId, id);
      sendSuccess(res, order, '订单已取消');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 模拟支付订单
  async payOrder(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      await orderModel.updateOrderStatus(userId, id, 'paid');
      const order = await orderModel.getOrderById(userId, id);
      sendSuccess(res, order, '订单支付成功');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 商家发货
  async shipOrder(req, res) {
    
    try {
      const { id: merchantId, role } = req.user;
      const { id } = req.params;
      
      if (role !== 'merchant') {
        return sendError(res, '只有商家可以发货', 403);
      }
      
      // 检查订单是否属于该商家
      await orderModel.getOrderForMerchant(merchantId, id);
      
      await orderModel.updateOrderStatus(merchantId, id, 'shipped');
      const order = await orderModel.getOrderForMerchant(merchantId, id);
      sendSuccess(res, order, '订单已发货');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 确认收货
  async deliverOrder(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      await orderModel.updateOrderStatus(userId, id, 'delivered');
      const order = await orderModel.getOrderById(userId, id);
      sendSuccess(res, order, '订单已确认收货');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 商家获取订单列表
  async getMerchantOrders(req, res) {
    try {
      const { id: merchantId, role } = req.user;
      const { page = 1, limit = 10 } = req.query;
      
      if (role !== 'merchant') {
        return sendError(res, '只有商家可以查看订单', 403);
      }
      
      const { orders, pagination } = await orderModel.getOrdersForMerchant(merchantId, page, limit);
      sendSuccess(res, { orders, pagination });
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 商家获取订单详情
  async getMerchantOrder(req, res) {
    try {
      const { id: merchantId, role } = req.user;
      const { id } = req.params;
      
      if (role !== 'merchant') {
        return sendError(res, '只有商家可以查看订单', 403);
      }
      
      const order = await orderModel.getOrderForMerchant(merchantId, id);
      sendSuccess(res, order);
    } catch (err) {
      sendError(res, err.message);
    }
  }
};